﻿using System;
using System.Collections.Generic;

namespace QntPlatform.Strategy.CmdCtrl
{
    public interface ICmdCtrl
    {
        bool Execute(CmdContext context = null);
    }
    public abstract class CmdHandlePart : StrategyPart
    {
       protected ILog log = Log.Default();
       // TvCmdHandler Parent{get;}
        public CmdHandlePart(IStrategy parent) : base(parent)
        {
            log=Log.GetLogger(this);
           // Parent=parent;
        }
        
         public T GetCmdValue<T>(string cmdValStr)
        {
            return SysFun.ParseTo<T>(cmdValStr);
        }
         public TrendSet trendSet;
        public int getTrend(int period)
        {
            if (trendSet == null)
            {
                trendSet = TrendSet.GetContextTrendSet(period, SysFun.GetRobotContext());
            }
            if (trendSet == null)
                return 0;
            return trendSet.GetTrend(period);
        }
       protected bool trendCheck(int period,bool isLong) {
            var trend = getTrend(period);
            if (isLong)
            {
                return trend is 1 or 2;
            }
            else
            {
                return trend is -1 or 2;
            }
        }

    }
    public interface INext<T, R>
    {
       R Invoke(T age);
    }
    public interface INext<T> : INext<T, object>
    { 
    }
    public  class MiddleNext<T, R> : INext<T, R>
    {
        public INext<T, R> Next { get; }
        public MiddleNext() { }
        public MiddleNext(INext<T, R> next) { Next = next; }
        public Func<T, R> Fun { get; set; }
        public R Invoke(T arg)
        {
            return Fun(arg);
        }

       
    }
    public  class MiddleNext<T> : MiddleNext<T,object>,INext<T>
    {
        
    }
    public class MiddleGuoup<T>
    {
         
        List<Func<INext<T>, INext<T>>> list=new List<Func<INext<T>, INext<T>>>();
        public Func<T, object> Bulid()
        {
            INext<T> nextObj = null;
            for (int i = list.Count - 1; i >= 0; i--)
            {
                nextObj = list[i].Invoke(nextObj);
            }
            return nextObj==null?null : nextObj.Invoke;
        }
        
        public MiddleGuoup<T> AddUse(Func<T, bool> fun)
        {
            list.Add(p => new MiddleNext<T>()
            {
                Fun = x =>
                {
                    try
                    {
                        if (!fun.Invoke(x)&&p!=null)
                            p.Invoke(x);
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Log.Default().Debug("回调错误",ex);
                        throw;
                    }
                }
            });
            return this;
        }
        public MiddleGuoup<T> AddUse(Func<INext<T>, INext<T>> fun)
        {
            //创建对象--》调用第一个
            list.Add(fun);
            return this;
        }
        public MiddleGuoup<T> AddRun(Action<T> fun)
        {
            list.Add(p => new MiddleNext<T>()
            {
                Fun = x =>
                {
                    fun.Invoke(x);
                    return true;
                }
            });
            return this;
        }
    }
    public class CmdContext : Dictionary<string, object>
    {
        public KeyValuePair<string, string> Cmd { get; set; }
        public string CmdStr { get; set; }
        public StrategyBase Parent { get; set; }


    }
}
